package com.tistory.wonsama.util;

import java.util.Collections;
import java.util.Comparator;
import java.util.List;

/**
 * 정렬
 * @author wonsama
 * @since 2012.01.09
 */
public class WSort {
	
	public static final int ASC = 1;
	public static final int DESC = -1;
	

	/**
	 * 숫자 정렬 (오름차순)
	 * @param list 숫자 목록
	 * @since 2012.01.09
	 */
	public static void sortInteger ( List<Integer>list )
	{
		sortInteger( list , ASC );
	}
	
	/**
	 * 숫자 정렬
	 * @param list 숫자 목록
	 * @param direction 방향
	 * @since 2012.01.09
	 */
	public static void sortInteger ( List<Integer>list , final int direction ){
		Comparator<Integer> comp = new Comparator<Integer>() {
			@Override
			public int compare(Integer o1, Integer o2) {
				if( o1 > o2 ){
					return direction;
				}else if ( o1 < o2 ){
					return - direction;
				}
				return 0;
			}
		};
		Collections.sort( list , comp );
	}
	
	/**
	 * 문자 정렬(오름차순)
	 * @param list 문자 목록
	 * @since 2012.01.09
	 */
	public static void sortString ( List<String> list )
	{
		sortString( list , ASC );
	}
	
	/**
	 * 문자 정렬
	 * @param list 문자 목록 
	 * @param direction 방향
	 */
	public static void sortString ( List<String> list , final int direction )
	{
		Comparator<String> comp = new Comparator<String>() {
			
			@Override
			public int compare(String o1, String o2) {
			
				if(o1 == null || o2 == null )
					return 0;
				
				if( direction == ASC ){
					return o1.compareTo( o2 );	
				}else{
					return o2.compareTo( o1 );
				}
			}
		};
		Collections.sort( list , comp );
	}
	
}
